﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using CVSS.Client.BizGIS.Business;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Geometry;
//using CVSS.Client.SC.Query;

using SysCommon.Entity.CVSS.WS.Stakeout;


namespace CVSS.Client.SC.Business.HistoryReplay
{
    public class HistoryReplayHelper
    {
        private int m_pointCount = 0;      //传入的gps定位点的数量
        private int m_timerInterval = 2000;   //时间间隔
        private CarHistory_Ety[] m_etys = null;   //历史回放实体
        private IElement[] m_preElement = null;     //前一个位置点元素
        private CVSS.Client.BizGIS.Business.Geometry m_geo =null;
        private AxMapControl m_mapControl = null;
        private Timer m_timer = null;
        private string m_carIcon = "";
        private int m_playInterval = 1;

        public delegate void ProcessHandler(CarHistory_Ety ety);
        public event ProcessHandler OnPlay;
        public event ProcessHandler EndPlay;

        #region 构造函数

        public HistoryReplayHelper()
        {
        }

        public HistoryReplayHelper(AxMapControl mapControl)
        {
            m_mapControl = mapControl;
        }
        #endregion

        #region 初始化

        /// <summary>
        /// 描述：类的初始化
        /// </summary>
        public void Initial()
        {
            try
            {
                SysCommon.Global.mainLog.Debug("开始调用Initial函数");
                if(m_geo==null)
                    m_geo = new Geometry(m_mapControl);

                m_timer=new Timer();
                //m_timerInterval =m_playInterval ;
                m_timer.Interval = m_timerInterval;
                m_timer.Enabled = false;

                m_timer.Tick += new EventHandler(m_timer_Tick);

                SysCommon.Global.mainLog.Debug("调用Initial函数结束");
            }
            catch (Exception ex)
            {
                SysCommon.Global.mainLog.Error("调用Initial函数出错", ex);
            }
        }
        
        /// <summary>
        /// 描述：类的初始化
        /// </summary>
        /// <param name="mapControl">地图控件</param>
        /// <param name="timer">时钟</param>
        /// <param name="carIcon">车辆图标</param>
        /// <param name="etys">历史回放实体</param>
        public void Initial(AxMapControl mapControl, Timer timer,string carIcon,CarHistory_Ety[] etys)
        {
            try
            {
                SysCommon.Global.mainLog.Debug("开始调用Initial函数");
                
                m_mapControl = mapControl;
                m_timer = timer;
                SetCarIcon(carIcon);
                SetCarHistoryEntity(etys);

                Initial();

                SysCommon.Global.mainLog.Debug("调用Initial函数结束");
            }
            catch (Exception ex)
            {
                SysCommon.Global.mainLog.Error("调用Initial函数出错", ex);
            }
        }
        #endregion

        #region 对应timer的tick事件

        void m_timer_Tick(object sender, EventArgs e)
        {
            try
            {
                SysCommon.Global.mainLog.Debug("开始调用m_timer_Tick函数");

                //
                m_timerInterval += m_timer.Interval;

                int tmpPointCount = m_pointCount - m_playInterval;
                if(tmpPointCount<m_etys.Length && m_pointCount>=m_etys.Length)
                {
                    m_pointCount = m_etys.Length - 1;
                    m_preElement[m_pointCount] = new BmpPictureElementClass();

                    int index = tmpPointCount;// m_pointCount - m_playInterval;
                    if (index <= 0) index = 0;
                    if (m_preElement[index] == null)
                    {
                        do
                        {
                            index--;
                            if (index == 0) break;
                        } while (m_preElement[index] == null);
                        //index = 0;
                    }

                    m_preElement[m_pointCount] = m_geo.UpdateCarPosition(m_preElement[index], m_etys[m_pointCount], m_mapControl);

                    OnPlay(m_etys[m_pointCount]);

                    m_pointCount = 0;
                    m_timer.Enabled = false;
                    EndPlay(m_etys[m_pointCount]);
                    return;
                }

                if (m_pointCount >= m_etys.Length)
                {
                    m_pointCount = 0;
                    m_timer.Enabled = false;
                    EndPlay(null);
                    return;
                }
                m_pointCount+=m_playInterval;

                if (m_timerInterval >=1000)
                {
                    m_timerInterval = 0;
                    if (m_pointCount < m_preElement.Length)
                    {
                        m_preElement[m_pointCount] = new BmpPictureElementClass();

                        int index = m_pointCount - m_playInterval;
                        if (index <= 0) index = 0;
                        if (m_preElement[index] == null)
                        {
                            do
                            {
                                index--;
                                if (index == 0) break;
                            } while (m_preElement[index] == null);
                            //index = 0;
                        }
                        m_preElement[m_pointCount] = m_geo.UpdateCarPosition(m_preElement[index], m_etys[m_pointCount], m_mapControl);
                        OnPlay(m_etys[m_pointCount]);
                    }
                }

                SysCommon.Global.mainLog.Debug("调用m_timer_Tick函数结束");
            }
            catch (Exception ex)
            {
                SysCommon.Global.mainLog.Error("调用m_timer_Tick函数出错", ex);
            }
        }

        #endregion

        #region 回放函数

        /// <summary>
        /// 描述：在地图上画路线
        /// </summary>
        public void DrawRoutine()
        {
            try
            {
                SysCommon.Global.mainLog.Debug("开始调用DrawRoutine函数");

                int count = m_etys.Length;

                IPointCollection ptColl = new PolylineClass();
                object ept = System.Reflection.Missing.Value;

                for (int i = 0; i < count - 1; i++)
                {
                    IPoint pt = new PointClass();
                    pt.PutCoords(m_etys[i].LONGITUDE,m_etys[i].LATITUDE);
                    ptColl.AddPoint(pt,ref ept,ref ept);
                }
                
                if(m_geo==null)
                    SetCarIcon(m_carIcon);
                //m_geo.Initial(m_carIcon);
                m_geo.DrawPolyline((IPolyline)ptColl, m_mapControl);
                m_geo.ZoomToGeometry((IPolyline)ptColl, m_mapControl);
                m_preElement[m_pointCount] = new BmpPictureElementClass();
                m_preElement[m_pointCount] = m_geo.DrawCar(m_etys[0],m_mapControl);

                SysCommon.Global.mainLog.Debug("调用DrawRoutine函数结束");
            }
            catch (Exception ex)
            {
                SysCommon.Global.mainLog.Error("调用DrawRoutine函数出错", ex);
            }
        }

        /// <summary>
        /// 描述：开始播放

        /// </summary>
        public void StartReplay()
        {
            try
            {
                SysCommon.Global.mainLog.Debug("开始调用StartReplay函数");

                //m_preElement[m_pointCount] = new BmpPictureElementClass();
                //m_preElement[m_pointCount] = m_geo.DrawCar(m_etys[0]);
                //m_pointCount++;
                m_timer.Enabled = true;

                SysCommon.Global.mainLog.Debug("调用StartReplay函数结束");
            }
            catch (Exception ex)
            {
                SysCommon.Global.mainLog.Error("调用StartReplay函数出错", ex);
            }
        }
        
        /// <summary>
        /// 描述：暂停回放

        /// </summary>
        public void PauseReplay()
        {
            m_timer.Enabled = false;
        }

        /// <summary>
        /// 描述：重新播放

        /// </summary>
        /// <param name="startIndex">重新开始播放的位置</param>
        public  void Replay(int startIndex)
        {
            try
            {
                SysCommon.Global.mainLog.Debug("开始调用Replay函数");
                if (startIndex < 0 || startIndex > m_etys.Length)
                    startIndex = 0;
                m_timer.Enabled = false;
                m_pointCount = startIndex;
                m_timer.Enabled = true;
                SysCommon.Global.mainLog.Debug("调用Replay函数结束");
            }
            catch (Exception ex)
            {
                SysCommon.Global.mainLog.Error("调用Replay函数出错", ex);
            }
        }
        #endregion

        #region 设置车辆图标\历史回放实体\回放速度

        /// <summary>
        /// 描述：设置车辆图标
        /// </summary>
        /// <param name="carIcon">车辆图标文件(*.bmp)路径</param>
        public void SetCarIcon(string carIcon)
        {
            try
            {
                SysCommon.Global.mainLog.Debug("开始调用SetCarIcon函数");

                if(m_geo!=null)
                {
                    m_geo = null;
                }
                m_carIcon = carIcon;
                m_geo=new Geometry(m_mapControl);
                m_geo.Initial(carIcon);

                SysCommon.Global.mainLog.Debug("调用SetCarIcon函数结束");
            }
            catch (Exception ex)
            {
                SysCommon.Global.mainLog.Error("调用SetCarIcon函数出错", ex);
            }
        }

        /// <summary>
        /// 描述：设置历史回放车辆实体

        /// </summary>
        /// <param name="etys">史回放车辆实体</param>
        public void SetCarHistoryEntity(CarHistory_Ety[] etys)
        {
            try
            {
                SysCommon.Global.mainLog.Debug("开始调用SetCarHistoryEntity函数");
                if(m_etys!=null)
                    m_etys = null;
                m_etys = etys;

                int count = m_etys.Length;
                if (m_preElement != null)
                {
                    for (int i = 0; i < m_preElement.Length; i++)
                    {
                        System.Runtime.InteropServices.Marshal.ReleaseComObject(m_preElement[i]);
                    }
                }
                m_preElement = new IElement[count];

                SysCommon.Global.mainLog.Debug("调用SetCarHistoryEntity函数结束");
            }
            catch (Exception ex)
            {
                SysCommon.Global.mainLog.Error("调用SetCarHistoryEntity函数出错", ex);
            }
        }

        /// <summary>
        /// 描述：设置播放速度
        /// </summary>
        /// <param name="playRate">播放速度(帧/秒)</param>
        /// <param name="isOnPlay">是否在播放状态</param>
        public void SetPlayInterval(int playRate,bool isOnPlay)
        {
            try
            {
                SysCommon.Global.mainLog.Debug("开始调用SetPlayInterval函数");
                if (m_timer == null) return;
                m_timer.Enabled = false;
                m_playInterval = playRate;
                //m_playInterval = 1000/playSpeed;
                //m_timer.Interval = m_playInterval;
                if(isOnPlay)
                    m_timer.Enabled = true;
                SysCommon.Global.mainLog.Debug("调用SetPlayInterval函数结束");
            }
            catch (Exception ex)
            {
                SysCommon.Global.mainLog.Error("调用SetPlayInterval函数出错", ex);
            }
        }
        #endregion 

        #region 属性

        /// <summary>
        /// 描述：时钟

        /// </summary>
        public Timer TIMER
        {
            get { return m_timer; }
            set { m_timer = value; }
        }

        /// <summary>
        /// 描述：地图控件

        /// </summary>
        public AxMapControl MAPCONTROL
        {
            set { m_mapControl = value; }
        }

        /// <summary>
        /// 描述：车辆图标文件路径

        /// </summary>
        public string CARICON
        {
            get { return m_carIcon; }
            set { m_carIcon = value; }
        }

        /// <summary>
        /// 描述：播放速度(帧/秒)
        /// </summary>
        public int PLAYRATE
        {
            get { return m_playInterval; }
            set { m_playInterval =value; }
        }

        /// <summary>
        /// 描述：当前播放进度

        /// </summary>
        public int POINTCOUNT
        {
            get { return m_pointCount; }
            //set { m_pointCount = value; }
        }

        #endregion
    }
}
